<template>
  <div class="roles container">
  <!--    面包屑导航-->
  <el-breadcrumb separator=">">
    <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item >权限管理</el-breadcrumb-item>
    <el-breadcrumb-item >角色列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!--    卡片区域-->
  <el-card class="box-card">
<!--    添加按钮-->
    <el-row>
      <el-col>
        <el-button type="primary">添加角色</el-button>
      </el-col>
    </el-row>
    <!--      表格数据渲染-->
    <el-table
        :data="RolesList"
        border
        stripe
        style="width: 100%">
      <el-table-column type="expand" >
        <!--      下拉内容-->
        <template slot-scope="props">
          <el-row
              v-for="(item1,i1) in props.row.children"
              :key="item1.id"
              :class="['exBottom',i1===0 ? 'exTop' : '']"
          >
<!--            24栅格布局-->
<!--            一级权限-->
            <el-col :span="5">
              <el-tag closable  @close="closeRolesTag(props.row,item1.id)">{{item1.authName}}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
<!--            二级和三级权限-->
            <el-col :span="19">
              <el-row
                  :class="[i2===0? '' : 'exTop']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
              >
<!--                二级权限-->
                <el-col :span="6">
                  <el-tag type="success" closable  @close="closeRolesTag(props.row,item2.id)">
                    {{item2.authName}}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
<!--                三级权限-->
                <el-col :span="18">
                  <el-tag closable
                          type="warning"
                          v-for="(item3,i3) in item2.children"
                          :key="item3.id"
                          @close="closeRolesTag(props.row,item3.id)"
                  >
                    {{item3.authName}}
                  </el-tag>
                </el-col>
              </el-row>
              </el-col>
          </el-row>
        </template>
      </el-table-column>


      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="roleName"  label="角色名称"></el-table-column>
      <el-table-column prop="roleDesc"  label="角色描述"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          <el-button type="warning" icon="el-icon-star-off" size="mini" @click="showSetRightsDialog(scope.row)">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<!--    分配权限对话框-->
    <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        >
<!--      树型控件-->
      <el-tree :data="rightsList"
               :props="rightsProps"
               show-checkbox
               node-key="id"
               default-expand-all
               :default-checked-keys="defKeys"
               ref="rightsTreeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="setRightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="sureSetRight">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRolesRights, rightsInfo, rolesManage, setRoles} from "@/utils/loginAPI";

export default {
  name: "Role",
  created() {
    this.getRolesList()
  },
  data(){
    return{
      RolesList:[],
      //分配权限
      setRightDialogVisible:false,
      rightsList:[],
      rightsProps:{
        label:'authName',
        children:'children'
      },
      defKeys:[],
      rolesId:'',
    }
  },
  methods:{
    async getRolesList(){
      const {data:res}=await rolesManage();
      this.RolesList=res.data;
      console.log(res);
    },
    // 删除角色指定权限
    closeRolesTag(roles,rightId){
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const {data:res}=await deleteRolesRights(roles,rightId);
        console.log(res);
        // 删除后重新渲染
        // this.getRolesList();
        roles.children=res.data;
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //分配权限
    async showSetRightsDialog(roles){
      this.rolesId=roles.id;
      const {data:res}=await rightsInfo('tree');
      if(res.meta.status!==200) return this.$message.error(res.meta.msg);
      this.rightsList=res.data;
      //在递归之前清空 不然会一直增加id
      this.defKeys=[];
      this.getLeafKeys(roles,this.defKeys);
      this.setRightDialogVisible=true;
    },
    //通过递归的形式，获取角色下所有的三级权限的id 并保存到 defKeys 中
    getLeafKeys(node,arr){

      //如果node不存在children属性 则是三级节点
      if(!node.children){
        return arr.push(node.id);
      }
      node.children.forEach(item=>{
        this.getLeafKeys(item,arr);
      })
    },
    async sureSetRight(rolesId){
      this.setRightDialogVisible = false;
      const keys=[
          ...this.$refs.rightsTreeRef.getCheckedKeys(),
          ...this.$refs.rightsTreeRef.getHalfCheckedKeys(),
      ];
      const keysStr=keys.join(',');
      // console.log(keysStr);
      //发起请求
      const {data:res}=await setRoles(this.rolesId,{rids:keysStr});
      if(res.meta.status!==200) return this.$message.error(res.meta.msg);
      // 重新渲染
      await this.getRolesList();
    }


  }

}
</script>

<style scoped lang="less">
  .exBottom{
    border-bottom: 1px solid #eee;
  }
  .exTop{
    border-top: 1px solid #eee;
  }
  .el-tag{
    margin: 8px;
  }
</style>
